import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../widgets/inputWidget.dart';
import '../../widgets/signatureCard2.dart';
import '../../helper/Helper.dart';
import '../../helper/cacheHelper.dart';
import '../../helper/channelHelper.dart';
import '../../helper/downloadHelper.dart';

import '../../helper/fileHelper.dart';
import '../../helper/uidata.dart';

import '../../widgets/signatureCard.dart';

import '../../helper/global_config.dart';

import '../../model/skddl2.dart';
import '../../widgets/IconWidget.dart';
import 'sdddl_authorty.dart';

class SKDDL2InfoHuiZhiPage extends StatefulWidget {
  @override
  SKDDL2InfoHuiZhiPage(this.s1, this.callback);
  final SKDDL2 s1;
  final RefrushCallback callback;

  @override
  createState() => new SKDDL2InfoHuiZhiPageState();
}

class SKDDL2InfoHuiZhiPageState extends State<SKDDL2InfoHuiZhiPage> {
  late SKDDL2 s1;
  final TextEditingController c2 = new TextEditingController();

  @override
  void initState() {
    super.initState();
    //s1 = widget.s1;
    //UIData.CurrentUser!.Authority="水调值班员,电厂值班员";
    loadData();
  }

  loadData() async {
    var s2 = await SKDDL2.Get(widget.s1.ID);
    setState(() {
      s1 = s2 as SKDDL2;
    });
  }

  upload(String imgId) async {
    if (s1.State == 6 && Helper.UserPB(s1.HZUserID)) {
      s1.HZIfOK = true;
      s1.HZQMTM = Helper.DateTimeNowString();
      s1.HZQM = imgId;
      s1.State = 7;

      await SKDDL2.Update(s1);
      var s2 = await SKDDL2.Get(s1.ID);
      if (s2 != null) {
        setState(() {
          s1 = s2;
        });
      }
      widget.callback();
      ChannelHelper.showToast("签字成功！");
      await SKDDL2.SendById(s1.Title, "完成操作回执！", s1.BMUserID);
    }
  }

  Widget getCLWidget(bool ifOk, String content, String imgId) {
    if (!ifOk)
      return ListTile(
          title: Text(
        "未处理",
        style: TextStyle(color: Colors.black54, height: 2.0),
      ));
    return ListTile(
      title: Text(
        content,
        style: TextStyle(color: Colors.black54, height: 2.0),
      ),
      trailing: Image.network(Helper.GetImgUrl(imgId)),
    );
  }

  List<Widget> getWidgetList() {
    List<Widget> list1 = <Widget>[];

    list1.add(InputWidget.getQMTile4(context, "执行回执审核", s1.State == 7,
        s1.HZUserID, s1.HZQM, Helper.DateTimeFormat(s1.HZTM)));

    return list1;
  }

  List<Widget> getWidgetList2() {
    List<Widget> list1 = <Widget>[];

    if (s1.State == 7) {
      list1.add(InputWidget.getDivider());
      list1.add(InputWidget.getInfo2("备注", s1.Record));
    }
    return list1;
  }

  void showPanel(BuildContext context) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return SignatureCard2Page(upload, "回执审核");
      },
    );
  }

  Widget getFloatingButton(BuildContext context) {
    if (s1 == null) return new Container();
    if (SkddlAuthority.IfQianZi2(s1, UIData.CurrentUser!)) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.penAlt),
          onPressed: () {
            showPanel(context);
          });
    }

    if (s1.State == 7) {
      return FloatingActionButton(
          child: Icon(FontAwesomeIcons.fileDownload),
          onPressed: () async {
            await DownloadHelper.DownLoadPDF(
                context,
                UIData.apiServer2 + "/ToPDF/SKDDL?id=" + s1.ID.toString(),
                s1.Title);
            await ChannelHelper.showToast("已下载，请到下载列表里查看");
          });
    }

    return new Container();
  }

  @override
  Widget build(BuildContext context) {
    // if (s1 == null) return Center(child:SpinKitCircle(color: Colors.blue,));

    // TODO: implement build
    return Scaffold(
        floatingActionButton: getFloatingButton(context),
        body: s1 == null
            ? Center(
                child: SpinKitCircle(
                color: Colors.blue,
              ))
            : s1.State < 6
                ? Center(child: Text("未执行"))
                : SingleChildScrollView(
                    child: new Container(
                        margin: EdgeInsets.only(top: 8.0),
                        child: new Column(children: <Widget>[
                          Card(
                            child: ListTile(
                              leading: IconWidget.SkddlImage(),
                              title: Text(s1.Title),
                            ),
                            color: GlobalConfig.cardBackgroundColor,
                            margin: const EdgeInsets.all(6.0),
                          ),
                          Card(
                            child: Column(
                              children: <Widget>[
                                ListTile(
                                  title: Text("调度令简述"),
                                ),
                                Divider(
                                  height: 0.0,
                                ),
                                ListTile(
                                  title: Text(
                                    s1.Content,
                                    style: TextStyle(
                                        color: Colors.black54, height: 2.0),
                                  ),
                                ),
                              ],
                            ),
                            color: GlobalConfig.cardBackgroundColor,
                            margin: const EdgeInsets.all(6.0),
                          ),
                          Card(
                            child: Column(
                              children: <Widget>[
                                ListTile(
                                  title: Text("执行情况回执"),
                                ),
                                Divider(
                                  height: 0.0,
                                ),
                                ListTile(
                                  title: Text(
                                    s1.HuiZhi,
                                    style: TextStyle(
                                        color: Colors.black54, height: 2.0),
                                  ),
                                ),
                                Divider(
                                  height: 0.0,
                                ),
                                ListTile(
                                  title: Text(
                                    Helper.DateTimeFormat(s1.HZTM),
                                    style: TextStyle(
                                        color: Colors.black54, height: 1.0),
                                  ),
                                  trailing: Text(
                                    s1.HZBXName,
                                    style: TextStyle(
                                        color: Colors.black54, height: 1.0),
                                  ),
                                ),
                              ],
                            ),
                            color: GlobalConfig.cardBackgroundColor,
                            margin: const EdgeInsets.all(6.0),
                          ),
                          Card(
                            child: Column(
                              children: getWidgetList(),
                            ),
                            color: GlobalConfig.cardBackgroundColor,
                            margin: const EdgeInsets.all(6.0),
                          ),
                          Card(
                            child: Column(
                              children: getWidgetList2(),
                            ),
                            color: GlobalConfig.cardBackgroundColor,
                            margin: const EdgeInsets.all(6.0),
                          ),
                        ])),
                  ));
  }
}
